package com.irdstudio.biz.rule.common.constant;

import org.apache.commons.lang.StringUtils;

public class Constant {

    public static final String SYS_CODE = "sys";

    /**
     * 是否枚举
     */
    public enum YES_OR_NO implements DictEnum {
        YES("1", "是"),
        NO("0", "否");
        private String code;
        private String name;

        private YES_OR_NO(String code, String name) {
            this.code = code;
            this.name = name;
        }

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public String getName() {
            return name;
        }

        public String toString() {
            return code + "-" + name;
        }
    }

    /**
     * 规则集状态
     */
    public enum RULE_COLL_STATE implements DictEnum {
        VALID("01", "启用"),
        INVALID("02", "停用");
        private String code;
        private String name;

        private RULE_COLL_STATE(String code, String name) {
            this.code = code;
            this.name = name;
        }

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public String getName() {
            return name;
        }

        public String toString() {
            return code + "-" + name;
        }
    }

    /**
     * 规则集类型
     */
    public enum RULE_COLL_TYPE implements DictEnum {
        Approval("01", "审批大纲"),
        SpecialApproval("02", "特批审批大纲"),
        Coli688("03", "中海规则"),
        ForeignTrade("04", "外贸规则");
        private String code;
        private String name;

        private RULE_COLL_TYPE(String code, String name) {
            this.code = code;
            this.name = name;
        }

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public String getName() {
            return name;
        }

        public String toString() {
            return code + "-" + name;
        }
    }

    /**
     * 规则适用类型
     */
    public enum ADAPTER_TYPE implements DictEnum {
        ALL("00", "全适用"),
        Approval("01", "审批大纲"),
        SpecialApproval("02", "特批审批大纲"),
        Management("03", "资方适用适用");
        private String code;
        private String name;

        private ADAPTER_TYPE(String code, String name) {
            this.code = code;
            this.name = name;
        }

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public String getName() {
            return name;
        }

        public String toString() {
            return code + "-" + name;
        }
    }

    private interface DictEnum {
        public String getCode();
        public String getName();
    }

    public static <T extends DictEnum> T get(Class<T> cls, String code) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        for (T t : cls.getEnumConstants()) {
            if (t.getCode().equals(code)) {
                return t;
            }
        }
        return null;
    }
}
